//将以上数据加载到程序中，按年龄升序输出，要求加载，排序，输出功能分别使用函数实现
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

typedef struct Studentdata
{
    int IID;
    char NName[30];
    int Aage;
    float Ssalary;
    struct Studentdata *ppre;
    struct Studentdata *pnext;
}STU;
//申请空间
STU *mkSTUNode()
{
	STU *stu = (STU*)malloc(sizeof(STU));
	if (NULL == stu)
	{
		exit(EXIT_FAILURE);
	}
	else
	{	
		memset(stu, 0, sizeof(STU));	
	}
	return stu;
}
//将文件内容加载在链表里
STU *mklist(const char *path)
{
    FILE *src1=fopen(path,"r");
    if(NULL==src1)
    {
        printf("errno=%d,error=%s\n",errno,strerror(errno));
        exit(EXIT_FAILURE);
    }
    printf("open %s ok\n",path);
    STU *head1=NULL;
    STU *node1=NULL;
    int ret=0;
    while(1)
    {
        node1=mkSTUNode();
        ret=fscanf(src1,"%d%s%d%f",&node1->IID,node1->NName,&node1->Aage,&node1->Ssalary);
        if(ret>0)
        {
            if(NULL == head1)
            {
                head1 = node1;
                //head1->pnext=node1;
               // node1->ppre=head1;
            }
            else
            {
                node1->pnext = head1;
                head1->ppre = node1;
                head1 = node1;
                #if 0
                node1->pnext=head1->pnext;
                head1->pnext->ppre=node1;
                head1->pnext=node1;
                node1->ppre=head1;
                #endif
            }
            
        }    
        else
        {
            free(node1);
            fclose(src1);
            return head1;
        }
        
    }
}
//交换
void myExchange(STU *a,STU *b)
{
    STU *temp;
    if(NULL ==a->ppre)
    {
        //temp->ppre=a->ppre;
        temp->pnext=a->pnext;
        a->ppre=b->ppre;
        if(NULL!=b->pnext)
        {
            a->pnext=b->pnext;
            a->pnext->ppre=a;
        }
        else
        {
            a->pnext=NULL;
        }
        b->ppre=NULL;
        b->pnext=temp->pnext;
        //b->ppre->pnext=b;
        temp=a;
        a=b;
        b=temp;
    }
    else
    {
        temp->ppre=a->ppre;
        temp->pnext=a->pnext;
        a->ppre=b->ppre;
        a->pnext=b->pnext;
        b->ppre=temp->ppre;
        b->pnext=temp->pnext;
        b->ppre->pnext=b;
        a->pnext->ppre=a;
        temp=a;
        a=b;
        b=temp;
    }
}
//按照年龄升序排列
void ShengxuAge(STU *head)
{
    STU *node=head->pnext;
    STU *flag=head;
    while(flag->pnext!=NULL)
    {
        while(node->pnext!=NULL)
        {
            if(flag->Aage>node->Aage)
            {
                myExchange(flag,node);
                printf("ok1\n");
            }
            node=node->pnext;
            printf("ok2\n");
        }
        printf("ok3\n");
        node=flag->pnext;
        printf("ok4\n");
        flag=flag->pnext;
        printf("ok5\n");
    }
    printf("ok6\n");
    while(flag->ppre!=NULL)
    {
        flag=flag->ppre;
    }
    head=flag;
    printf("ok3\n");
}
//输出
void display(STU *head)
{
	if (NULL == head)
	{
		return;
	}
	
	STU *node = head->pnext;
	while (NULL != node)
	{
		printf("id:%d,name:%s,age:%d,salary:%.1f\n", node->IID
			   , node->NName, node->Aage,node->Ssalary);
		node = node->pnext;
	}
}
int main(int argc,char*argv[])
{
    if(2!=argc)
    {
        printf("参数有问题……\n");
        exit(EXIT_FAILURE);
    }
    
    STU *newList=mklist(argv[1]);
    display(newList);
    printf("\n------------------------------\n");
    ShengxuAge(newList);
    printf("ok \n");
    display(newList);

    return 0;
}
